﻿using Mengsk.ProxyDelivery.Note.ViewModels;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.Note
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private NoteService noteService = ServiceContainer.GetService<NoteService>();
        private System.Collections.ObjectModel.ObservableCollection<NoteViewModel> notes = new System.Collections.ObjectModel.ObservableCollection<NoteViewModel>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.dgvItems.ItemsSource = this.notes;
        }

        private NoteViewModel GetSelectedViewModel()
        {
            var item = this.dgvItems.SelectedItem as NoteViewModel;
            if (item == null)
            {
                MessageBox.Show("请选择要处理的项");
            }
            return item;
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var window = new CreateNoteWindow();
                bool? ret = window.ShowDialog();
                if (ret != null && ret.Value)
                {
                    this.btnQuery_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnComplete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var noteViewModel = this.GetSelectedViewModel();
                if (noteViewModel == null)
                {
                    return;
                }
                if (MessageBox.Show("是否处理完成", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                {
                    return;
                }
                this.noteService.MarkState(noteViewModel.Source.Id, Domain.NoteState.Complete);
                noteViewModel.State = Domain.NoteState.Complete;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var noteViewModel = this.GetSelectedViewModel();
                if (noteViewModel == null)
                {
                    return;
                }

                if (MessageBox.Show("是否删除", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                {
                    return;
                }
                this.noteService.Delete(noteViewModel.Source.Id);
                this.notes.Remove(noteViewModel);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var datas = ServiceContainer.GetService<NoteService>().GetByAll("", this.cbbStates.SelectedIndex, 0, 0);
                var items = datas.Datas.Select(obj => new ViewModels.NoteViewModel(obj)).ToArray();
                this.notes.Clear();
                foreach (var item in items)
                {
                    this.notes.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
